﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Services;
using System.Collections;

namespace Catering.Catering_Page.system
{
    public partial class AreaRules_Edit : System.Web.UI.Page
    {
        public int m_ARID = 0;
        public string m_States = "添加";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["ARID"] != null) Int32.TryParse(Request.QueryString["ARID"].ToString(), out m_ARID);
            if (m_ARID > 0)
            {
                m_States = "修改";
            }
            if (!IsPostBack)
            {
                string rawids = "";
                DataTable dt_Area = new Catering_Rules.Area().GetAllList().Tables[0];
                this.Drop_AreaID.LoadDrop(dt_Area, "id", "Name");
                this.Drop_AreaID.SelectedIndex = 0;
                if (m_ARID > 0)
                {
                    Catering_Model.AreaRules md = new Catering_Rules.AreaRules().GetModel(m_ARID);
                    if (md != null)
                    {
                        rawids = md.RawIDs;
                        txt_ARName.Text = md.ARName;
                        Drop_AreaID.SelectedValue = md.AreaID.ToString();
                        HF_ARID.Value = md.ARID.ToString();

                    }
                }
                InitRawNames(rawids);
            }
        }

        public void InitRawNames(string RawIDs)
        {
            StringBuilder result = new StringBuilder("");
            DataTable dt = new Catering_Rules.T_RawMaterials().GetAllList().Tables[0];
            string[] SplitRawIDs = RawIDs.Split(',');
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int id = Int32.Parse(dt.Rows[i]["id"].ToString());
                    string TypeName = dt.Rows[i]["TypeName"].ToString();
                    DataTable dt_Raw = new Catering_Rules.T_RawMaterial().GetList("RAWID=" + id, "id,RAWName").Tables[0];
                    if (dt_Raw != null && dt_Raw.Rows.Count > 0)
                    {
                        bool flag = true;
                        result.Append("<dl class='raw_dl'>");
                        result.Append("<dt class='raw_dt'><input type='checkbox' id='RawT_" + id + "' ◇ onclick=SetCheckMore(this,'" + id + "') />" + TypeName + "</dt>");
                        for (int j = 0; j < dt_Raw.Rows.Count; j++)
                        {
                            int tempid = Int32.Parse(dt_Raw.Rows[j]["id"].ToString());
                            string rawName = dt_Raw.Rows[j]["RAWName"].ToString();
                            if (SplitRawIDs.Contains(tempid.ToString()))
                            {
                                result.Append("<dd class='raw_dd'><input type='checkbox' RAW='RawType' name='Raw_" + id + "' id='" + tempid + "' checked='checked' value='" + tempid + "' />" + rawName + "</dd>");
                            }
                            else
                            {
                                flag = false;
                                result.Append("<dd class='raw_dd'><input type='checkbox' RAW='RawType' name='Raw_" + id + "' id='" + tempid + "' value='" + tempid + "'  />" + rawName + "</dd>");
                            }
                        }
                        dt_Raw.Dispose();
                        result.Append("</dl>");
                        if (flag) result.Replace("◇", " checked='checked' "); else result.Replace("◇", "");
                    }
                }
                dt.Dispose();
            }
            RawNamesStr.InnerHtml = result.ToString();
        }

        //保存原料
        [WebMethod]
        public static ArrayList btn_Save(string ARID, string ARName, string AreaID, string RawIDlist)
        {
            int id = 0;
            Catering_Model.AreaRules md = new Catering_Model.AreaRules();
            md.AreaID = Convert.ToInt32(AreaID);
            md.ARName = ARName;
            md.RawIDs = RawIDlist.TrimEnd(',');
            Int32.TryParse(ARID, out id);
            if (id > 0)
            {
                md.ARID = id;
                new Catering_Rules.AreaRules().Update(md);
            }
            else
            {
                id = new Catering_Rules.AreaRules().Add(md);
            }
            ArrayList al = new ArrayList();            
            al.Add("OK");
            al.Add(id);
            return al;
        }
    }
}